﻿using Caliburn.Micro;
using MusicExplorer.Logging;
using MusicExplorer.ViewModels;
using MusicExplorer.Views;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// Pour plus d'informations sur le modèle Application vide, consultez la page http://go.microsoft.com/fwlink/?LinkId=234227

namespace MusicExplorer
{
    /// <summary>
    /// Fournit un comportement spécifique à l'application afin de compléter la classe Application par défaut.
    /// </summary>
    public sealed partial class App
    {
        private WinRTContainer container;

        public App()
        {
            InitializeComponent();
        }

        protected override void Configure()
        {
            LogManager.GetLog = t => new DebugLog();

            container = new WinRTContainer();
            container.RegisterWinRTServices();

            container.RegisterSharingService();

            var settingsService = container.RegisterSettingsService();

            //settingsService.RegisterFlyoutCommand<SampleSettingsViewModel>("Custom");
            //settingsService.RegisterUriCommand("View Website", new Uri("http://caliburnmicro.codeplex.com"));

            container
                .PerRequest<RootViewModel>()
                .PerRequest<HomeViewModel>()
                .PerRequest<RepositoryViewModel>();
            //    .PerRequest<BindingsViewModel>()
            //    .PerRequest<CoroutineViewModel>()
            //    .PerRequest<ExecuteViewModel>()
            //    .PerRequest<MenuViewModel>()
            //    .PerRequest<NavigationTargetViewModel>()
            //    .PerRequest<NavigationViewModel>()
            //    .PerRequest<SampleSettingsViewModel>()
            //    .PerRequest<SearchViewModel>()
            //    .PerRequest<SettingsViewModel>()
            //    .PerRequest<SetupViewModel>()
            //    .PerRequest<ShareSourceViewModel>()
            //    .PerRequest<ShareTargetViewModel>()
            //    .PerRequest<ConventionsViewModel>()
            //    .PerRequest<HubViewModel>();
        }

        protected override object GetInstance(Type service, string key)
        {
            var instance = container.GetInstance(service, key);
            if (instance != null)
                return instance;

            throw new Exception("Could not locate any instances.");
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            container.BuildUp(instance);
        }

        //protected override void PrepareViewFirst(Frame rootFrame)
        //{
        //    container.RegisterNavigationService(rootFrame);
        //}

        protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
            DisplayRootView<RootView>();
        }

        //protected override void OnSearchActivated(SearchActivatedEventArgs args)
        //{
        //    DisplayRootView<SearchView>(args.QueryText);
        //}

        //protected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args)
        //{
        //    // Normally wouldn't need to do this but need the container to be initialised
        //    Initialize();

        //    // replace the share operation in the container
        //    container.UnregisterHandler(typeof(ShareOperation), null);
        //    container.Instance(args.ShareOperation);

        //    DisplayRootViewFor<ShareTargetViewModel>();
        //}
    }
}
